// pages/list/list.js
var app = getApp()
const $api = require('../../utils/api.js').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件所需的参数
    navbarData: {
      havebg: 1, //1表示不显示    0表示显示
      out: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '产品列表', //导航栏 中间的标题
      bg: '#fff', //导航背景
    },
    totalPrice: '0.00', //合计价格
    height: app.globalData.height,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    //获取二次配货产品列表
    var _this = this;
    _this.setData({
      is_prod_user: options.is_prod_user, //1==跳转查看   0==正常跳转
      ds: options.ds
    })

  },

  onShow() {
    var _this = this;
    var ds = _this.data.ds;
    if (ds == 1) {
      $api.getDcGoods({
        login_token: wx.getStorageSync('login_token'),
      }).then((res) => {
        var list = res.data.data;
        for (var i = 0; i < list.length; i++) {
          list[i].iconState = false;
          list[i].num = 1;
        }
        _this.setData({
          list: list,
          loadModal: false,
        })
      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })
    } else if (ds == 3) {//获取活动产品列表
      $api.getGoodshuodong({
        login_token: wx.getStorageSync('login_token'),
      }).then((res) => {
        var list = res.data.data;
        for (var i = 0; i < list.length; i++) {
          list[i].iconState = false;
          list[i].num = 1;
          list[i].is_buy = 0;
        }
        _this.setData({
          list: list,
          loadModal: false,
        })
      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })
    } else {
      $api.getGoods({
        login_token: wx.getStorageSync('login_token'),
      }).then((res) => {
        var list = res.data.data;
        for (var i = 0; i < list.length; i++) {
          list[i].iconState = false;
          list[i].num = 1;
          list[i].is_buy = 0;
        }
        _this.setData({
          list: list,
          loadModal: false,
        })
      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })
    }
  },
  //商品数量+1
  onAdd(e) {
    var _this = this;
    var idx = e.currentTarget.dataset.idx;
    var list = _this.data.list;
    _this.setData({
      ['list[' + idx + '].num']: parseInt(list[idx].num) + 1,
    })
    var shopPrice = 0;
    for (var i = 0; i < list.length; i++) {
      if (list[i].iconState) {
        shopPrice = shopPrice + parseFloat(list[i].price) * (parseFloat(list[i].num));
      }
    }
    shopPrice = shopPrice.toFixed(2)
    _this.setData({
      totalPrice: shopPrice,
    })

  },
  //商品数量-1
  onReduce(e) {
    var _this = this;
    var idx = e.currentTarget.dataset.idx;
    var list = _this.data.list;
    if (parseInt(list[idx].num) - 1 <= 1) {
      _this.setData({
        ['list[' + idx + '].num']: 1
      })
    } else {
      _this.setData({
        ['list[' + idx + '].num']: parseInt(list[idx].num) - 1,
      })
    }
    var shopPrice = 0;
    for (var i = 0; i < list.length; i++) {
      if (list[i].iconState) {
        shopPrice = shopPrice + parseFloat(list[i].price) * (parseFloat(list[i].num));
      }
    }
    shopPrice = shopPrice.toFixed(2)
    _this.setData({
      totalPrice: shopPrice,
    })

  },
  //选择商品
  onChoiceShop(e) {
    var _this = this;
    var idx = e.currentTarget.dataset.idx;
    var list = _this.data.list;
    var shopPrice = 0;
    var is_buy = e.currentTarget.dataset.is_buy;
    if (is_buy == 0) {
      if (list[idx].iconState) {
        _this.setData({
          ['list[' + idx + '].iconState']: false
        })
      } else {
        _this.setData({
          ['list[' + idx + '].iconState']: true
        })
      }
      for (var i = 0; i < list.length; i++) {
        if (list[i].iconState) {
          shopPrice = shopPrice + parseFloat(list[i].price) * parseFloat(list[i].num);
        }
      }
      console.log(shopPrice)
      shopPrice = shopPrice.toFixed(2)
      _this.setData({
        totalPrice: shopPrice,
      })
    }

  },
  //提交订单
  onReading() {
    wx.removeStorageSync('address')
    wx.removeStorageSync('applyShop')
    wx.removeStorageSync('wait_pay')
    wx.removeStorageSync('giftId')
    wx.removeStorageSync('pay')
    wx.removeStorageSync('goodsList')
    var goods = [];
    var list = this.data.list;
    for (var i = 0; i < list.length; i++) {
      if (list[i].iconState) {
        goods = goods.concat(list[i])
      }
    }
    let is = 0
    let id = 0
    goods.forEach((item) => {
      if (item.is_register == 0) {
        is++
      }
      if (item.is_register == 2) {
        id++
      }
    })
    if (is && id) {
      wx.showToast({
        title: '请选择不同商品类型商品',
        icon: 'none',
        duration: 2000
      })
      return
    }
    console.log(goods)
    if (goods.length <= 0) {
      wx.showToast({
        title: '请选择您要购买的商品',
        icon: 'none',
        duration: 2000
      })
    } else {
      goods = JSON.stringify(goods)
      let is_register = is ? 0 : 2
      wx.navigateTo({
        url: '/pages/reading/reading?state=3&&twoType=1&&goods=' + goods + "&is_register=" + is_register,
      })
      wx.removeStorageSync('addressid')
    }
  },

  showModal(e) {
    console.log(e.currentTarget.dataset.text)
    this.setData({
      goodstext: e.currentTarget.dataset.text,
      modalName: e.currentTarget.dataset.target,

    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
})